<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->session->sess_destroy();
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_login_view');
		$this->load->view('admin/b_footer_view');
	}
	
	function process()
	{
		$this->load->helper(array('form', 'security'));
		$this->load->library('form_validation');
		$this->load->model('admin/admin_model','admin_model');
		
		$this->form_validation->set_rules('username', 'username', 'required');
		$this->form_validation->set_rules('password', 'password', 'required');
		$this->form_validation->set_error_delimiters('<span style="font-size:10pt; color:#FF0000;font-style:italic">','</span>');
		$this->form_validation->set_message('required', ' ');
		$msg = null;
		
		if($this->form_validation->run())
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			
			$data = $this->admin_model->getUserByUsername($username);
			if($data != null && $data['username'] == $username)
			{
				if($password == $data['password']){
					$newdata = array(
						'username'  => $username,
						'level'   => $data['level'],
						'admin_logged_in' => TRUE
					);
					
					$this->session->set_userdata($newdata);
					redirect(base_url().'admin/main');
				}
				else
				{
					$this->session->set_flashdata('message', 'username หรือ รหัสผ่านไม่ถูกต้อง');
					redirect(base_url().'admin/login');
				}
			}
			else
			{
				$this->session->set_flashdata('message', 'username หรือ รหัสผ่านไม่ถูกต้อง');
				redirect(base_url().'admin/login');
			}
		}else{
			redirect(base_url().'admin/login');
		}
	}
}

/* End of file login.php */
/* Location: ./application/controllers/admin/login.php */